/**
 * 年月日选择
 * @param {'选择器'} selector
 * @param {'开始日期'} startDate
 * @param {'结束年'} endDate
 * @param {'回调'} callback
 * @returns {MobileSelect}
 */
function dateSelectV2(selector, startDate, endDate, callback) {
    // alert(startDate + "" + endDate)
    // let date = new Date()
    let startYear = !startDate ? 1970 : startDate.getFullYear()
    let endYear = !endDate ? 2100 : endDate.getFullYear()
    if (startYear && endYear && (startYear > endYear)) {
        return false
    }
    let startMouth = !startDate ? 1 : startDate.getMonth() + 1
    let endMouth = !endDate ? 12 : endDate.getMonth() + 1

    let startDay = !startDate ? 1 : startDate.getDate()
    let endDay = !endDate ? -1 : endDate.getDate()

    var fullData = getFullDataV2(startYear, startMouth, startDay, endYear, endMouth, endDay)
    var position = getPosition(fullData);

    return new MobileSelect({
        trigger: selector,
        title: '选择时间',
        triggerDisplayData: false,
        wheels: fullData,
        position: position,
        callback: function (a, b) {
            if (callback !== undefined) {
                callback({year: b[0].id, month: b[1].id, day: b[2].id})
            }
        }
    })
}

/**
 * 年月日选择
 * @param {'选择器'} selector
 * @param {'开始年'} startYear
 * @param {'结束年'} endYear
 * @param {'回调'} callback
 * @returns {MobileSelect}
 */
function dateSelect(selector, startYear, endYear, callback) {
    if (!startYear && endYear) {
        startYear = endYear - 20
    }
    if (startYear && !endYear) {
        endYear = new Date().getFullYear()
    }
    if (startYear && endYear && (startYear > endYear)) {
        return false
    }
    var fullData = getFullData(startYear, endYear)
    var position = getPosition(fullData);

    return new MobileSelect({
        trigger: selector,
        title: '选择时间',
        triggerDisplayData: false,
        wheels: fullData,
        position: position,
        callback: function (a, b) {
            if (callback !== undefined) {
                callback({year: b[0].id, month: b[1].id, day: b[2].id})
            }
        }
    })
}


function yearSelect(selector, startYear, endYear, callback) {
    if (!startYear && endYear) {
        startYear = endYear - 20
    }
    if (startYear && !endYear) {
        endYear = new Date().getFullYear()
    }
    if (startYear && endYear && (startYear > endYear)) {
        return false
    }
    const position = [10, 0, 0];
    new MobileSelect({
        trigger: selector,
        title: '选择时间',
        wheels: function (startYear, endYear) {
            let fullData = [{data: []}];
            for (fullData = [{data: []}]; startYear <= endYear; startYear++) {
                let yearData = {id: startYear, value: startYear + "年"};
                fullData[0].data.push(yearData)
            }
            return fullData
        }(startYear, endYear),
        position: position,
        callback: function (a, b) {
            if (callback !== undefined) {
                callback({year: b[0].id})
            }
        }
    })
}

function getFullDataV2(startYear, startMouth, startDay, endYear, endMouth, endDay) {
    let startYearTemp = startYear
    let fullData = [{data: []}];
    for (fullData = [{data: []}]; startYear <= endYear; startYear++) {
        let yearData = {id: startYear, value: startYear + "年", childs: []};
        let mouthData = (startYearTemp === startYear) ? startMouth : 1;
        let fullMouth = (startYear === endYear && mouthData === endMouth) ? endMouth : 12
        for (; fullMouth >= mouthData; mouthData++) {
            fullMouth = (startYear === endYear && mouthData === endMouth) ? endMouth : 12
            let mouthChilds = [];
            let dayData = (startYearTemp === startYear && mouthData === startMouth) ? startDay : 1;
            let dayNumber = (1 === mouthData || 3 === mouthData || 5 === mouthData || 7 === mouthData || 8 === mouthData || 10 === mouthData || 12 === mouthData) ? 31 : 2 === mouthData ? 0 === startYear % 4 && 0 !== startYear % 100 ? 29 : 0 === startYear % 400 ? 29 : 28 : 30;
            dayNumber = (startYear === endYear && mouthData === endMouth) ? (endDay > 0 ? endDay : dayNumber) : dayNumber;
            for (; dayData <= dayNumber; dayData++) {
                mouthChilds.push({
                    id: dayData < 10 ? '0' + dayData : dayData,
                    value: (dayData < 10 ? '0' + dayData : dayData) + "日"
                });
            }
            yearData.childs.push({
                id: mouthData < 10 ? '0' + mouthData : mouthData,
                value: (mouthData < 10 ? '0' + mouthData : mouthData) + "月",
                childs: mouthChilds < 10 ? '0' + mouthChilds : mouthChilds
            })
        }
        fullData[0].data.push(yearData)
    }
    return fullData
}

function getFullData(startYear, endYear) {
    let fullData = [{data: []}];
    for (fullData = [{data: []}]; startYear <= endYear; startYear++) {
        let yearData = {id: startYear, value: startYear + "年", childs: []};
        let mouthData = 1;
        for (; 12 >= mouthData; mouthData++) {
            let mouthChilds = [];
            let dayData = 1;
            let dayNumber = (1 === mouthData || 3 === mouthData || 5 === mouthData || 7 === mouthData || 8 === mouthData || 10 === mouthData || 12 === mouthData) ? 31 : 2 === mouthData ? 0 === startYear % 4 && 0 !== startYear % 100 ? 29 : 0 === startYear % 400 ? 29 : 28 : 30;
            for (; dayData <= dayNumber; dayData++) {
                mouthChilds.push({
                    id: dayData < 10 ? '0' + dayData : dayData,
                    value: (dayData < 10 ? '0' + dayData : dayData) + "日"
                });
            }
            yearData.childs.push({
                id: mouthData < 10 ? '0' + mouthData : mouthData,
                value: (mouthData < 10 ? '0' + mouthData : mouthData) + "月",
                childs: mouthChilds < 10 ? '0' + mouthChilds : mouthChilds
            })
        }
        fullData[0].data.push(yearData)
    }
    return fullData
}

function getPosition(fullData) {
    var position = []
    var y = new Date().getFullYear()
    var m = new Date().getMonth() + 1
    var d = new Date().getDate()
    m = m < 10 ? '0' + m : m
    d = d < 10 ? '0' + d : d
    var yIndex = fullData[0].data.findIndex(item => {
        return item.value == (y + '年')
    })
    position.push(yIndex)
    var mIndex = fullData[0].data[yIndex].childs.findIndex(item => {
        return item.value == (m + '月')
    })
    position.push(mIndex)
    var dIndex = fullData[0].data[yIndex].childs[mIndex].childs.findIndex(item => {
        return item.value == (d + '日')
    })
    position.push(dIndex)
    return position
}
